package thread.synchronizedtest.test03;

/**
 * 任意对象放在成员变量的位置  同一个对象  就只有一个线程可以访问
 * 如果放在方法内部则变为异步执行
 *
 * */
public class ObjectService {
    private String uname;
    private String pwd;
    String lock=new String();
    public void setUserNamePassWord(String userName,String passWord){
        try {
            synchronized (lock) {
                System.out.println("thread name="+Thread.currentThread().getName()
                        +" 进入代码快:"+System.currentTimeMillis());
                uname=userName;
                Thread.sleep(3000);
                pwd=passWord;
                System.out.println("thread name="+Thread.currentThread().getName()
                        +" 进入代码快:"+System.currentTimeMillis()+"入参uname:"+uname+"入参pwd:"+pwd);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}